package com.qqtech.qquan.pub.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.hi.service.HiShopService;
import com.qqtech.qquan.mall.model.MallProduct;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallProductService;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.pub.model.PubConcern;
import com.qqtech.qquan.pub.model.PubConcernQuery;
import com.qqtech.qquan.pub.model.PubConcernVo;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserTopic;
import com.qqtech.qquan.user.service.UserTopicService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/pub/concern")
public class PubConcernController extends BaseWebController<PubConcern, PubConcernQuery> {
	@Resource
	private PubConcernService pubConcernService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private UserTopicService userTopicService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private MallProductService mallProductService;
	@Resource
	private MallShopService mallShopService;
	@Resource
	private HiShopService hiShopService;

	@Override
	protected BaseService<PubConcern> getBaseService() {
		return pubConcernService;
	}
	
	/**
	 * 我的关注
	 * 
	 * @param pubCommentQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody PubConcernQuery pubConcernQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		pubConcernQuery.setCreatorId(curUserId);
		Pageable pageKit = new PageKit(pubConcernQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<PubConcernVo> page = pubConcernService.queryPageList(pubConcernQuery, pageKit);
		for(PubConcernVo concern : page.getContent()){
			if(BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(concern.getBizzType())){
				CircleTopic topic = circleTopicService.getById(concern.getBizzId());
				if (topic != null) {
					concern.setBizzContent(topic.getContent());
					concern.setBizzImg(topic.getImg());
				} 
			}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(concern.getBizzType())){
				CommunityMessage msg =  communityMessageService.getById(concern.getBizzId());
				if(msg != null){
					concern.setBizzContent(msg.getContent());
					concern.setBizzImg(msg.getImg());
				}
			}else if(BizzLevel2Enum.MALL_PRODUCT.getCode().equals(concern.getBizzType())){
				MallProduct product =  mallProductService.getById(concern.getBizzId());
				if(product != null){
					concern.setBizzContent(product.getName());
					concern.setBizzImg(product.getImg());
				}
			}
			concern.setImgString(concern.getBizzImg(),SysConst.IMG_POSTFIX);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 添加page
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody PubConcern pubConcern) {
		Result result = new Result();
		Pageable pageKit = new PageKit(pubConcern.getPage(), new PageOrder(Direction.ASC, "id"));
		Page<PubConcernVo> page = pubConcernService.queryPageList(pubConcern, pageKit);
		result.put("page", page);
		return result;
	}

	/**
	 * 添加list
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/list")
	@ResponseBody
	public Result list(@RequestBody PubConcern pubConcern) {
		Result result = new Result();
		List<PubConcernVo> list = pubConcernService.queryList(pubConcern);
		result.put("list", list);
		return result;
	}

	/**
	 * 添加关注
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doAdd")
	@ResponseBody
	public Result doAdd(@RequestBody PubConcern pubConcern) {
		Result result = new Result();
		// 查看是否已存在关注信息
		pubConcern.setCreatorId(super.getUserId());
		PubConcern concern = pubConcernService.getOne(pubConcern, new PageOrder());
		if (concern == null) {
			if(pubConcern.getBizzType().equalsIgnoreCase(BizzLevel2Enum.MALL_PRODUCT.getCode())){
				mallProductService.increaseConcern(pubConcern.getBizzId(),1);//商品收藏（关注）数量+1
			}
			Integer beUserId = pubConcernService.getBeUserId(pubConcern.getBizzId(), pubConcern.getBizzType());
			pubConcern.setBeUserId(beUserId);
			int concernId = pubConcernService.insert(pubConcern);
			if (concernId > 0) {
				if (BizzLevel2Enum.QSHOP_SHOP.getCode().equals(pubConcern.getBizzType())) {
					MallShop shop = mallShopService.getById(pubConcern.getBizzId());
					//店铺收藏得荣誉值,收藏本人创建的店铺不计算在内
					if(shop != null && shop.getCreatorId().intValue() != pubConcern.getCreatorId().intValue()){
						hiShopService.collection(pubConcern.getCreatorId(), pubConcern.getBizzId());	
					}
				}
			}
			result.put("concernId", concernId);
		} else {
			// 返回提示信息
			//result.addErr("已关注");
		}
		return result;
	}

	/**
	 * 取消关注
	 * 
	 * @param pubConcern
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doCancle")
	@ResponseBody
	public Result doCancle(@RequestBody PubConcern pubConcern) {
		Result result = new Result();
		// 查看是否已存在关注信息
		pubConcern.setCreatorId(super.getUserId());
		PubConcern concern = pubConcernService.getOne(pubConcern);
		if (concern == null) {
			// 返回提示信息
		} else {
			pubConcernService.delete(pubConcern);
			// 其他清理业务
			if(concern.getBizzType().equalsIgnoreCase(BizzLevel2Enum.MALL_PRODUCT.getCode())){
				mallProductService.increaseConcern(concern.getBizzId(), -1);//商品收藏（关注）数量-1
			}
		}
		return result;
	}

	/**
	 * 返回关注状态
	 * 
	 */
	@RequestMapping(value = "/getPubConcern")
	@ResponseBody
	public Result getPubConcern(@RequestBody PubConcern pubConcern) {
		Result result = new Result();
		// 查看是否已存在关注信息
		PubConcern concern = pubConcernService.getOne(pubConcern);
		if (concern != null) {
			result.put("id", concern.getId());
		}
		return result;
	}
	
	/**
	 * 我的收藏
	 * @param pubConcernQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/myFavorite")
	@ResponseBody
	public Result myFavorite(@RequestBody PubConcernQuery pubConcernQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		pubConcernQuery.setCreatorId(curUserId);
		List<String> includeBizzTypes = Lists.newArrayList();
		includeBizzTypes.add(BizzLevel2Enum.USER_TOPIC.getCode());
		includeBizzTypes.add(BizzLevel2Enum.CIRCLE_TOPIC.getCode());
		includeBizzTypes.add(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
		includeBizzTypes.add(BizzLevel2Enum.MALL_PRODUCT.getCode());
		includeBizzTypes.add(BizzLevel2Enum.QSHOP_SHOP.getCode());
		pubConcernQuery.setIncludeBizzTypes(includeBizzTypes);
		Pageable pageKit = new PageKit(pubConcernQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<PubConcernVo> page = pubConcernService.queryPageList(pubConcernQuery, pageKit);
		for(PubConcernVo concern : page.getContent()){
			if(BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(concern.getBizzType())){
				CircleTopic topic = circleTopicService.getById(concern.getBizzId());
				if (topic != null) {
					concern.setBizzContent(topic.getContent());
					concern.setBizzImg(topic.getImg());
				} 
			}else if(BizzLevel2Enum.USER_TOPIC.getCode().equals(concern.getBizzType())){
				UserTopic topic = userTopicService.getById(concern.getBizzId());
				if (topic != null) {
					concern.setBizzContent(topic.getContent());
					concern.setBizzImg(topic.getImg());
				} 
			}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(concern.getBizzType())){
				CommunityMessage msg =  communityMessageService.getById(concern.getBizzId());
				if(msg != null){
					concern.setBizzContent(msg.getContent());
					concern.setBizzImg(msg.getImg());
				}
			}else if(BizzLevel2Enum.MALL_PRODUCT.getCode().equals(concern.getBizzType())){
				MallProduct product =  mallProductService.getById(concern.getBizzId());
				if(product != null){
					concern.setBizzContent(product.getName());
					concern.setBizzImg(product.getImg());
				}
			}else if(BizzLevel2Enum.QSHOP_SHOP.getCode().equals(concern.getBizzType())){
				MallShopVo shop = mallShopService.getById(concern.getBizzId());
				if(shop != null){
					concern.setBizzContent(shop.getName());
					concern.setBizzImg(shop.getLogo());
				}
			}
			concern.setImgString(concern.getBizzImg(),SysConst.IMG_POSTFIX);
		}
		result.put("page", page);
		return result;
	}

}
